﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace TheOliver.Controls
{
    //
    // If you want your Action to target elements other than its parent, extend your class
    // from TargetedTriggerAction instead of from TriggerAction
    //
	public class DeepZoom_ZoomOutSlideShow : TargetedTriggerAction<MultiScaleImage>
	{
        private MultiScaleImage msi;
        private int count = 0;
        private int totalImages;

        public DeepZoom_ZoomOutSlideShow()
		{

		}

		protected override void Invoke(object o)
		{
            if (this.msi == null)
            {
                this.msi = this.Target;
                totalImages = msi.SubImages.Count;
            }

            // Insert code that defines what the Action will do when triggered/invoked.
            if (count < totalImages)
            {
                ZoomAllTheWayOut();
            }
            else
            {
                count = 0;
                ZoomAllTheWayOut();
            }
		}

        private void ZoomAllTheWayOut()
        {
            this.msi.ViewportWidth = 150;
            this.msi.ViewportOrigin = new Point(-50, -50);
            this.msi.MotionFinished += new RoutedEventHandler(msi_MotionFinished);
        }

        void msi_MotionFinished(object sender, RoutedEventArgs e)
        {
            ZoomOnImage(count);
            this.msi.MotionFinished -= new RoutedEventHandler(msi_MotionFinished);
        }

        private void ZoomOnImage(int subImageIndex)
        {
            count++;
            //
            // Thanks to Ken Perkins of the Live Labs team for
            // helping me with portions of the below code
            //

            // Get current SubImage
            MultiScaleSubImage subImage = msi.SubImages[subImageIndex];

            // Get the current zoom factor to scale everything by
            double scaleBy = 1 / subImage.ViewportWidth;

            // Create a Rect of your SubImage's coordinates and size in the current zoom level
            Rect subImageRect = new Rect(-subImage.ViewportOrigin.X * scaleBy,
                                         -subImage.ViewportOrigin.Y * scaleBy,
                                         scaleBy,
                                         (1 / subImage.AspectRatio) * scaleBy);

            // Figure out how to align the image inside the MSI viewport
            if ((this.msi.Width / this.msi.Height) > subImage.AspectRatio)
            {
                double targetViewportWidth = subImageRect.Height * this.msi.AspectRatio;
                double targetPointX = (targetViewportWidth - subImageRect.Width) / 2;

                this.msi.ViewportOrigin = new Point(subImageRect.X - targetPointX, subImageRect.Y);
                this.msi.ViewportWidth = targetViewportWidth;
            }
            else
            {
                this.msi.ViewportWidth = subImageRect.Width;
                this.msi.ViewportOrigin = new Point(subImageRect.X, subImageRect.Y);
            }
        }
    }
}